package com.example.weatherforecast;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.weatherforecast.database.MyDataBaseManager;

public class MoreActivity extends AppCompatActivity implements View.OnClickListener {

    // 控件
    TextView bgTv, cacheTv, versionTv, shareTv;
    RadioGroup exbgRg;
    ImageView backIv;

    // 存放背景壁纸的名称
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);

        init();
    }

    private void init() {
        // 初始化控件
        bgTv = findViewById(R.id.moreTvExchangeBg);
        cacheTv = findViewById(R.id.moreTvCache);
        versionTv = findViewById(R.id.moreTvVersion);
        shareTv = findViewById(R.id.moreTvShare);
        backIv = findViewById(R.id.moreIvBack);
        exbgRg = findViewById(R.id.moreRg);

        //设置事件监听器
        bgTv.setOnClickListener(this);
        cacheTv.setOnClickListener(this);
        shareTv.setOnClickListener(this);
        backIv.setOnClickListener(this);

        pref = getSharedPreferences("bg_pref", MODE_PRIVATE);

        String versionName = getVersionName();
        versionTv.setText("当前版本:    v" + versionName);

        setRGListener();
    }

    /**
     * 设置按钮监听器，根据所选按钮更换壁纸
     */
    private void setRGListener() {
        // 设置按钮监听器
        exbgRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取目前的默认壁纸
                int bg = pref.getInt("bg", 0);
                // 获取写入对象
                SharedPreferences.Editor editor = pref.edit();
                switch (checkedId) {
                    case R.id.moreRbBlue:
                        if (bg == 0) {
                            Toast.makeText(MoreActivity.this, "您选择的为当前背景，无需改变！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg", 0);
                        editor.commit();
                        break;
                    case R.id.moreRbGreen:
                        if (bg == 1) {
                            Toast.makeText(MoreActivity.this, "您选择的为当前背景，无需改变！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg", 1);
                        editor.commit();
                        break;
                    case R.id.moreRbPink:
                        if (bg == 2) {
                            Toast.makeText(MoreActivity.this, "您选择的为当前背景，无需改变！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg", 2);
                        editor.commit();
                        break;
                }
                // 选择壁纸后跳转到主界面
                Intent intent = new Intent(MoreActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    /**
     * 获取应用的版本号
     *
     * @return 版本号
     */
    public String getVersionName() {
        PackageManager manager = getPackageManager();
        String versionName = null;
        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.moreIvBack:
                finish();
                break;
            case R.id.moreTvCache:// 清除缓存
                clearCache();
                break;
            case R.id.moreTvShare:
                shareSoftwareMsg("天气app是一款天气预报软件，画面简约，播报天气情况非常精准，快来下载吧！");
                break;
            case R.id.moreTvExchangeBg: //更换壁纸
                // 根据RadioGroup状态进行展开还是关闭
                if (exbgRg.getVisibility() == View.VISIBLE) {
                    exbgRg.setVisibility(View.GONE);
                } else {
                    exbgRg.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    /**
     * 分享软件
     *
     * @param s
     */
    private void shareSoftwareMsg(String s) {
        // 创建分享意图
        Intent intent = new Intent(Intent.ACTION_SEND);
        // 数据类型
        intent.setType("text/plain");
        // 添加数据到意图
        intent.putExtra(Intent.EXTRA_TEXT, s);
        // 跳转
        startActivity(Intent.createChooser(intent, "天气"));
    }

    /**
     * 清除缓存，即将数据库里的数据清除
     */
    private void clearCache() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("确定要删除所有缓存么？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 清除数据库数据
                MyDataBaseManager.deleteAllInfo();
                Toast.makeText(MoreActivity.this, "已清除全部缓存！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MoreActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }).setNegativeButton("取消", null);
        builder.create().show();
    }
}
